package com.wugh.fast.module.web;

import com.wugh.fast.module.web.converter.DateConvert;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

/**
 * 描述: SpringMVC 自动处理 字符串类型的前后空白
 *
 * @author wugh
 * @date 2018-09-04 15:42
 */
@ControllerAdvice
public class  RequestParameterConverter implements WebBindingInitializer {
    @Getter
    @Setter
    private Boolean trim;


    @Override
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        if (Boolean.TRUE.equals(trim)) {
            //字符串去除空白
            binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
        }
        //日期多格式自动转换
        binder.registerCustomEditor(Date.class, new DateConvert());
    }
}
